<div class="row animated fadeInRight" id="orgIndex">
	<ol class="breadcrumb">
		<#if pMenu??>
		<li>${pMenu.name}</li> </#if>
		<li class="active">${menu.name}</li>
	</ol>
	<div class="button-group">
		<button class="btn btn-default btn-lg btn-add"
			@click="showView('add')">添加</button>
		<button class="btn btn-default btn-lg btn-edit"
			@click="showView('edit')">编辑</button>
	</div>
	<div class="col-sm-5">
		<div id="tree"></div>
	</div>
	<div class="col-sm-7">
		
	</div>
</div>

<script>
	var vm = new Vue({
		el : "#orgIndex",
		data : {
			resc:"org",
			curCode:"1000",
			treeData:[]
		},
		ready : function() {
			this.load();
		},
		methods : {
			load:function(){
				this.$http.get("/backend/org/tree").then(function(resp) {
					var items=resp.data.items;
					handleSubNodes(items);
					this.treeData=items;
					this.draw();
				});
			},
			showView:function(type){
				var _this=this;
				var options = {
			        url: "/backend/view?resc="+this.resc+"&type="+type+"&code="+this.curCode,
			        title:'提示对话框'
			    };
				eModal.ajax(options).done(function(html){
					_this.viewLoaded(html);
				});
			},
			viewLoaded:function(html){
				var _this=this;
				var $form=$(html).find("form").first();
				$form.on("submit",function(){
					$form.ajaxSubmit({
		                success:function(resp){
		                	if(resp.errCode=='00'){
		                		_this.treeData=resp.items;
								_this.$nextTick(function () {
									_this.draw();
								});
		                	}else{
		                		eModal.alert(resp.message);
		                	}
		                },
		                error:function(){
		                	eModal.alert("提交失败");
		                }
		            });
					return false;
				});
			},
			update:function(){
				this.treeData=tree;
				this.draw();
			},
			draw:function(){
				var _this=this;
				$("#tree").empty();
				$('#tree').treeview({
					data: this.treeData,
					showTags:true,
					//nodeIcon: "glyphicon glyphicon-user",
					levels:5,
					onNodeSelected: function(event, data){
						_this.curCode=data.tags[0];
					}
				});
			}
		}
	});

	function handleSubNodes(nodes){
		nodes.forEach(function(node){
			if(node.nodes.length>0){
				handleSubNodes(node.nodes);
			}else{
				delete node.nodes;
			}
		});
	}
	
	
</script>